home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 17.4 KB | 612 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // CWDebug.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #if defined(__MWERKS__)
-
- // Metrowerks
-
- #ifndef __CWDEBUG__
- #include "CWDebug.h"
- #endif
-
- #ifndef __CONSOLE__
- #include <console.h>
- #endif
-
- #ifndef __SIOUX__
- #include <SIOUX.h>
- #endif
-
- #if (qDebug || qTheDebugger)
-
- // MacApp
-
- #ifndef __UDEBUG__
- #include "UDebug.h"
- #endif
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- // Toolbox
-
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
-
- #ifndef __AEREGISTRY__
- #include <AERegistry.h>
- #endif
-
- #ifndef __AEOBJECTS__
- #include <AEObjects.h>
- #endif
-
- #ifndef __AEOBJECTPACKING__
- #include <AEPackObject.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- #undef Inherited
-
- /****************************************************************
-
- The following is support for debugging with CW builds
- Mark Anderson
- metrowerks
- 1/2/95
-
- InitUDebug has been changed slightly so that it sets
- a variable which indicates which debugger is being used.
- If a debugger is being used that supports output to a
- console-type window, as with MWDebug or Jasik, the following
- code will do that. You can manually override this default
- behavior in one of 2 ways. If you want to use SIOUX for output,
- you will need to add the SIOUX library to your project and set
- TypeOfCWDebugging to equal kxUseSIOUX. (TypeOfCWDebugging is set in
- CWDebug.h.) If you want your debugger to always use the AppleEvent
- method of reporting, set TypeOfCWDebugging to kxUseSTE.
-
- MacApp has trouble with SIOUX because its window isn't
- part of the MacApp controlled window list. Consequently
- window activation can get confused. We leave it in because
- it is an alternative. Perhaps MacApp will someday be
- friendlier to non-MacApp windows.
-
- If the Debugger Nub is running (MetroNUB, Debug Services, etc.),
- you must have an appropriate debugger running that can
- intercept the SysBreakStrs. You must also have STE open
- if you intend to use the AppleEvent hack. Even though
- SourceBug can't currently read CW Sym files, you can use
- it to output to if you want but it is a little funky.
-
- ****************************************************************/
-
- long AEfprintf(char *buffer, long n);
-
- #pragma segment GDebug
-
- //----------------------------------------------------------------------------------------
- // InitCWDebug:
- //----------------------------------------------------------------------------------------
-
- void InitCWDebug()
- {
- #if TypeOfCWDebugging == kxUseSIOUX
- // Please see SIOUX.h for the meanings of these fields
- SIOUXSettings.initializeTB = FALSE;
- SIOUXSettings.setupmenus = FALSE;
- SIOUXSettings.autocloseonquit = TRUE;
- SIOUXSettings.asktosaveonclose = TRUE;
- SIOUXSettings.showstatusline = FALSE;
- SIOUXSettings.tabspaces = 0;
- SIOUXSettings.columns = 80;
- SIOUXSettings.rows = 24;
- SIOUXSettings.toppixel = 0;
- SIOUXSettings.leftpixel = 0;
- SIOUXSettings.fontid = monaco;
- SIOUXSettings.fontsize = 9;
- SIOUXSettings.fontface = normal;
- #endif
-
- setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
- }
-
- #if TypeOfCWDebugging == kxUseSIOUX
- short CWDebugHandleOneEvent(struct EventRecord* userevent)
- {
- return SIOUXHandleOneEvent(userevent);
- }
- #else
- short CWDebugHandleOneEvent(struct EventRecord* /*userevent*/)
- {
- return 0;
- }
- #endif
-
- #if TypeOfCWDebugging != kxUseSIOUX
-
- short InstallConsole(short /* fd */)
- {
- return noErr;
- }
-
- long WriteCharsToConsole(char *buffer, long n)
- {
- if (n > 0)
- {
- #if TypeOfCWDebugging == kxUseSTE
- return AEfprintf(buffer, n);
- #endif
- DebugWriteLnHook(buffer, n);
- return n;
- }
- return 0;
- }
-
- long ReadCharsFromConsole(char * /* buffer */, long /* n */)
- {
- return 0;
- }
-
- //========================================================================================
- // CLASS XAEApplication
- //========================================================================================
-
- // The following code is for sending AppleEvents to Scriptable Text Editor
-
- class XAEApplication
- {
- protected:
- AEDesc fAppObj;
- public:
- XAEApplication( void );
- ~XAEApplication( void );
- virtual AEDesc * GetObject(void) { return &fAppObj; }
- };
-
- //========================================================================================
- // CLASS XAEWindow
- //========================================================================================
-
- class XAEWindow : public XAEApplication
- {
- protected:
- AEDesc fWindObj;
- public:
-
- XAEWindow( void );
- ~XAEWindow( void );
- virtual AEDesc * GetObject(void) { return &fWindObj; }
- };
-
- //========================================================================================
- // CLASS XAEParagraph
- //========================================================================================
-
- class XAEParagraph : public XAEWindow
- {
- protected:
- AEDesc fParaObj;
- public:
- XAEParagraph() { ProgramBreak("XAEParagraph called without parameters");}
- XAEParagraph( long windowNumber );
- ~XAEParagraph( void );
- virtual AEDesc * GetObject(void) { return &fParaObj; }
- };
-
- //========================================================================================
- // CLASS XAppleEvent
- //========================================================================================
-
- class XAppleEvent
- {
- AppleEvent fEvent;
- public:
- XAppleEvent() { ProgramBreak("XAppleEvent called without parameters");}
- XAppleEvent( OSType appid, OSType eventclass, OSType eventid );
- XAppleEvent( OSType appid, OSType eventclass, OSType eventid, XAEApplication* directObject );
- OSErr AddIndirectObject( AEKeyword theKeyword, DescType type, Ptr thing, Size size );
- OSErr Send();
- OSErr GetResponseFromReply( OSType desiredType, Ptr thing, Size size );
- long GetNumOfThings( void );
- Boolean DoesThingExist( void );
- ~XAppleEvent(void);
- };
-
- //constants
- const long badReturnValue = -1L;
-
- //prototypes
- Boolean sendMessageToWrite (OSType appid, OSType eventclass, OSType eventid, char * tmp) ;
- Boolean FindAProcess( OSType signature );
- Boolean AppleEventsAvailable( void );
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::XAppleEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment GDebug
-
- XAppleEvent::XAppleEvent( OSType appid, OSType eventclass, OSType eventid )
- {
- AEAddressDesc address = {typeNull, nil};
- OSErr err;
-
- fEvent.descriptorType = typeNull;
- fEvent.dataHandle = nil;
-
- err = AECreateDesc (typeApplSignature, (Ptr) &appid, sizeof (appid), &address);
- if ( err == noErr )
- err = AECreateAppleEvent ( eventclass, eventid, &address, 0, 0, &fEvent);
- if ( err != noErr )
- ProgramBreak("Failed in XAppleEvent constructor");
-
- AEDisposeDesc (&address);
-
- }
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::XAppleEvent:
- //----------------------------------------------------------------------------------------
-
- XAppleEvent::XAppleEvent( OSType appid, OSType eventclass, OSType eventid, XAEApplication* directObject )
- {
- AEAddressDesc address = {typeNull, nil};
- OSErr err;
-
- fEvent.descriptorType = typeNull;
- fEvent.dataHandle = nil;
-
- err = AECreateDesc (typeApplSignature, (Ptr) &appid, sizeof (appid), &address);
- if ( err == noErr )
- err = AECreateAppleEvent ( eventclass, eventid, &address, 0, 0, &fEvent);
- if ( err == noErr )
- err = AEPutParamDesc (&fEvent, keyDirectObject, directObject->GetObject() );
- if ( err != noErr )
- ProgramBreak("Failed in XAppleEvent constructor");
-
- AEDisposeDesc (&address);
-
- }
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::AddIndirectObject:
- //----------------------------------------------------------------------------------------
-
- OSErr XAppleEvent::AddIndirectObject( AEKeyword theKeyword, DescType type, Ptr thing, Size size )
- {
- OSErr err;
- AEDesc indirect = {typeNull, nil};
-
-
- err = AECreateDesc(type, thing, size, &indirect);
-
- if ( err == noErr )
- err = AEPutParamDesc (&fEvent, theKeyword, &indirect);
-
- if ( err != noErr )
- ProgramBreak("Failed in AddIndirectObject");
-
- AEDisposeDesc (&indirect);
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::Send:
- //----------------------------------------------------------------------------------------
-
- OSErr XAppleEvent::Send( void )
- {
- AppleEvent reply = {typeNull, nil};
- OSErr err;
-
- err = AESend ( &fEvent, &reply, kAENoReply , kAENormalPriority, kNoTimeOut, nil, nil);
-
- AEDisposeDesc (&reply);
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::~XAppleEvent:
- //----------------------------------------------------------------------------------------
-
- XAppleEvent::~XAppleEvent( void )
- {
- AEDisposeDesc (&fEvent);
- }
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::GetResponseFromReply:
- //----------------------------------------------------------------------------------------
-
- OSErr XAppleEvent::GetResponseFromReply( OSType desiredType, Ptr thing, Size size )
- {
-
- AppleEvent reply = {typeNull, nil};
- OSErr err;
- DescType actualtype;
- Size actualsize;
-
-
- err = AESend ( &fEvent, &reply, kAEWaitReply + kAENeverInteract,
- kAENormalPriority, 120, nil, nil);
- if ( err == noErr )
- err = AEGetParamPtr ( &reply, keyDirectObject, desiredType,
- &actualtype, thing, size, &actualsize);
- if ( err != noErr )
- ProgramBreak("Failed in GetResponseFromReply");
-
-
- AEDisposeDesc (&reply);
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::GetNumOfThings:
- //----------------------------------------------------------------------------------------
-
- long XAppleEvent::GetNumOfThings( void )
- {
-
- OSErr err;
- long things = badReturnValue;
-
- err = GetResponseFromReply( typeLongInteger, (Ptr) &things, sizeof (things) );
-
- if ( err != noErr )
- ProgramBreak("Failed in GetNumOfThings");
-
- return things;
- }
-
- //----------------------------------------------------------------------------------------
- // XAppleEvent::DoesThingExist:
- //----------------------------------------------------------------------------------------
-
- Boolean XAppleEvent::DoesThingExist( void )
- {
-
- OSErr err;
- Boolean thingExists = false;
-
- err = GetResponseFromReply( typeBoolean, (Ptr) &thingExists, sizeof (thingExists) );
-
- if ( err != noErr )
- ProgramBreak("Failed in DoesThingExist");
-
- return thingExists;
- }
-
- //----------------------------------------------------------------------------------------
- // XAEApplication::XAEApplication:
- //----------------------------------------------------------------------------------------
-
- XAEApplication::XAEApplication ( void )
- {
-
- fAppObj.descriptorType = typeNull;
- fAppObj.dataHandle = nil;
-
- }
-
- //----------------------------------------------------------------------------------------
- // XAEApplication::~XAEApplication:
- //----------------------------------------------------------------------------------------
-
- XAEApplication::~XAEApplication(void)
- {
-
- AEDisposeDesc (&fAppObj);
-
- }
-
- //----------------------------------------------------------------------------------------
- // XAEWindow::XAEWindow:
- //----------------------------------------------------------------------------------------
-
- XAEWindow::XAEWindow ( void ) : XAEApplication( )
- {
- OSErr myErr;
- AEDesc windNumDesc = {typeNull, nil};
- long windowNumber;
-
- fWindObj.descriptorType = typeNull;
- fWindObj.dataHandle = nil;
- //we only deal with the frontmost window
- windowNumber = 1;
- //the container is Null
- myErr = AECreateDesc(typeLongInteger,(Ptr)&windowNumber, sizeof(windowNumber), &windNumDesc);
- myErr = CreateObjSpecifier ( cWindow, &fAppObj, formAbsolutePosition, &windNumDesc, true, &fWindObj );
-
- }
-
- //----------------------------------------------------------------------------------------
- // XAEWindow::~XAEWindow:
- //----------------------------------------------------------------------------------------
-
- XAEWindow::~XAEWindow(void)
- {
-
- AEDisposeDesc (&fWindObj);
-
- }
-
- //----------------------------------------------------------------------------------------
- // XAEParagraph::XAEParagraph:
- //----------------------------------------------------------------------------------------
-
- XAEParagraph::XAEParagraph ( long paraNumber ) : XAEWindow( )
- {
-
- OSErr myErr;
- AEDesc paraNumDesc = {typeNull, nil};
-
- fParaObj.descriptorType = typeNull;
- fParaObj.dataHandle = nil;
- //the container is Null
- myErr = AECreateDesc(typeLongInteger,(Ptr)¶Number, sizeof(paraNumber), ¶NumDesc);
- myErr = CreateObjSpecifier ( cParagraph, &fWindObj, formAbsolutePosition, ¶NumDesc, true, &fParaObj );
-
- }
-
- //----------------------------------------------------------------------------------------
- // XAEParagraph::~XAEParagraph:
- //----------------------------------------------------------------------------------------
-
- XAEParagraph::~XAEParagraph(void)
- {
- AEDisposeDesc (&fParaObj);
- }
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // sendMessageToWrite:
- //----------------------------------------------------------------------------------------
-
- Boolean sendMessageToWrite (OSType appid, OSType eventclass, OSType eventid, char * txt)
- {
-
- long paragraphNum;
- Boolean result = false;
- OSErr err;
- OSType indirect = cParagraph;
-
- XAEWindow windObj;
- XAppleEvent count( appid, kAECoreSuite, kAECountElements, &windObj );
- count.AddIndirectObject( keyAEObjectClass, typeType, (Ptr)&indirect, sizeof(indirect) );
- paragraphNum = count.GetNumOfThings();
-
- if ( paragraphNum != badReturnValue )
- {
- //make an apple event for changing the text in the last paragraph
- XAEParagraph paraObj( paragraphNum );
- XAppleEvent writeIt( appid, eventclass, eventid, ¶Obj );
- writeIt.AddIndirectObject( keyAEData, typeChar, txt, strlen(txt) );
- err = writeIt.Send();
- if (noErr == err )
- {
- //make a apple event for selecting the text in the last paragraph (so it scrolls)
- paragraphNum = count.GetNumOfThings();// call because the number of paras has changed
- XAEParagraph paraObj2( paragraphNum - 1 ); //we want to select the last line
- //with a word; not the return character
- XAppleEvent selectIt( appid, kAEMiscStandards, kAESelect, ¶Obj2 );
- err = selectIt.Send();
- }
- }
-
- return noErr == err;
-
- } /*sendMessageToWrite*/
-
- //----------------------------------------------------------------------------------------
- // FindAProcess:
- //----------------------------------------------------------------------------------------
-
- Boolean FindAProcess( OSType signature )
- {
- Boolean result = false;
- ProcessSerialNumber PSN;
- ProcessInfoRec infoRec;
- PSN.highLongOfPSN = 0;
- PSN.lowLongOfPSN = kNoProcess;
- OSType application = 'APPL';
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = nil;
- infoRec.processAppSpec = nil;
-
- while (GetNextProcess( &PSN ) == noErr )
- {
- if ( GetProcessInformation( &PSN, &infoRec ) == noErr )
- if ( (infoRec.processType == application) &&
- (infoRec.processSignature == signature ) )
- {
- result = true;
- break;
- }
- }
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // AEfprintf:
- //----------------------------------------------------------------------------------------
-
- long AEfprintf(char *buffer, long byteCount)
- {
-
- char debugStr[256];
- static Boolean firstTime = true;
- OSType signature = 'quil'; //Although this code doesn't use unusual AEs (mostly from
- //core suite), the only app I know that will work is Scriptable
- //Text Editor (which you will find on the CD with the AppleScript
- //installation stuff).
-
- memcpy(debugStr, buffer, (size_t) byteCount);
- debugStr[byteCount] = 0;
-
- if ( !FindAProcess( signature ) )
- {
- if (firstTime)
- {
- ProgramBreak( "\pLaunch Scriptable Text Editor to display Debug Messages, etc." );
- firstTime = false;
- }
- return byteCount;
- }
- //well, it's running, but does it currently have a window?
- XAEWindow windObj; //looking for a window
- XAppleEvent exists( signature, kAECoreSuite, kAEDoObjectsExist, &windObj );
-
- if ( !exists.DoesThingExist() )
- {
- //The app's running but there isn't a window. Let's make it easy and open one ourselves.
- OSType indirect = cWindow;
- //This AE doesn't have a direct parameter
- XAppleEvent make( signature, kAECoreSuite, kAECreateElement );
- make.AddIndirectObject( keyAEObjectClass, typeType, (Ptr)&indirect, sizeof(indirect) );
- if (make.Send())
- {
- ProgramBreak( "\pWasn't able to make a window for STE." );
- return byteCount;
- }
- }
-
- sendMessageToWrite ( signature , kAECoreSuite, kAESetData, debugStr );
-
- return byteCount;
- }
-
- #endif //OnlyUseSIOUX
- #endif //(qDebug || qTheDebugger)
-
- #if TypeOfCWDebugging != kxUseSIOUX
- void RemoveConsole(void)
- {
- return;
- }
- #endif
-
- #endif // defined(__MWERKS__)
-
- //----------------------------------------------------------------------------------------
- // End of CWDebug.cp
-
- #pragma segment Inline
-